////////////////////////////////////////////////////////////////////////////////////////////////////
/// Copyright (c) 2009 - 2010 RacoonStudios
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this
/// software and associated documentation files (the "Software"), to deal in the Software
/// without restriction, including without limitation the rights to use, copy, modify, merge,
/// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
/// to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or
/// substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
/// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
/// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
/// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
/// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
/// DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once
#ifndef ringarray_h__
#define ringarray_h__

#include "core/types.h"
#include "helper/array.h"

namespace Helper
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// \class	RingArray
	///
	/// \brief	RingArray. 
	///
	///	\todo 	Add documentation.
	///
	/// \author	khaos
	/// \date	05.01.2010
	////////////////////////////////////////////////////////////////////////////////////////////////////
	template<class T>
	class RingArray
	{
		public:

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	RingArray()
			///
			/// \brief	Constructor. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			////////////////////////////////////////////////////////////////////////////////////////////////////
			RingArray();

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	RingArray(tSize capacity)
			///
			/// \brief	Constructor. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	capacity	The capacity. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			RingArray(tSize capacity);

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	RingArray(const RingArray<T> & rhs)
			///
			/// \brief	Constructor. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	rhs	The right hand side object. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			RingArray(const RingArray<T> & rhs);

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	~RingArray()
			///
			/// \brief	Finaliser. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			////////////////////////////////////////////////////////////////////////////////////////////////////
			~RingArray();




			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	void operator= (const RingArray<T> & rhs)
			///
			/// \brief	Copy operator. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	rhs	The right hand side object. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void operator = (const RingArray<T> & rhs);

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	T & operator[] (tIndex index) const
			///
			/// \brief	 casting operator. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	index	The index of the element we want. 
			///
			/// \return	The element at the specified index.
			////////////////////////////////////////////////////////////////////////////////////////////////////
			T & operator [] (tIndex index) const;




			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	void SetCapacity(tSize capacity)
			///
			/// \brief	Sets the capacity of the Stack.
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	capacity	The capacity. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void SetCapacity(tSize capacity);

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	tSize Capacity() const
			///
			/// \brief	Returns the capacity of the Stack.
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	The capacity of the Stack.
			////////////////////////////////////////////////////////////////////////////////////////////////////
			tSize Capacity() const;

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	tSize Size() const
			///
			/// \brief	Returns the size of the Stack.
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	The size of the Stack.
			////////////////////////////////////////////////////////////////////////////////////////////////////
			tSize Size() const;

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	bool IsEmpty() const
			///
			/// \brief	Determines if the Stack is empty.
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	True if the Stack is empty, otherwise false.
			////////////////////////////////////////////////////////////////////////////////////////////////////
			bool IsEmpty() const;

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	void Reset()
			///
			/// \brief	Resets the Stack.
			///
			/// \author	khaos
			/// \date	05.01.2010
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void Reset();




			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	void Add(const T & element)
			///
			/// \brief	Adds element.
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	element	The const T & to add. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void Add(const T & element);

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	T & Front() const
			///
			/// \brief	Fronts this object. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			T & Front() const;

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	T & Back() const
			///
			/// \brief	Backs this object. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			T & Back() const;




			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	Array<T> GetAsArray() const
			///
			/// \brief	Gets as array. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	as array. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			Array<T> GetAsArray() const;

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	T * GetBuffer()
			///
			/// \brief	Gets the buffer. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	null if it fails, else the buffer. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			T * GetBuffer();

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	const T * GetBuffer() const
			///
			/// \brief	Gets the buffer. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \return	null if it fails, else the buffer. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			const T * GetBuffer() const;

		protected:

		private:

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	protected: private: void Alloc(tSize capacity)
			///
			/// \brief	Allocs. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	capacity	The capacity. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void Alloc(tSize capacity);

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	void Delete()
			///
			/// \brief	Deletes the whole Stack.
			///
			/// \author	khaos
			/// \date	05.01.2010
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void Delete();

			////////////////////////////////////////////////////////////////////////////////////////////////////
			/// \fn	void Copy(const RingArray<T> & rhs)
			///
			/// \brief	Copies the given rhs. 
			///
			/// \author	khaos
			/// \date	05.01.2010
			///
			/// \param	[in]	rhs	The right hand side object. 
			////////////////////////////////////////////////////////////////////////////////////////////////////
			void Copy(const RingArray<T> & rhs);



			tSize m_capacity;
			tSize m_size;
			tIndex m_baseIndex;
			tIndex m_headIndex;

			T * m_elements;

	};





	///
	template<class T>
	void RingArray<T>::Alloc(tSize capacity)
	{
		if(capacity > 0)
		{
			this->m_elements = r_narray(T,capacity);

			this->m_capacity = capacity;
			this->m_size = 0;
			this->m_baseIndex = 0;
			this->m_headIndex = 0;
		}
	}


	///
	template<class T>
	void RingArray<T>::Delete()
	{

	}


	///
	template<class T>
	void RingArray<T>::Copy(const RingArray<T> & rhs)
	{
		if(this != &rhs)
		{
			this->Delete();

			this->m_elements = r_narray(T, rhs.m_capacity);

			this->m_baseIndex = rhs.m_baseIndex;
			this->m_headIndex = rhs.m_headIndex;
			this->m_size = rhs.m_size;

			for(tIndex i = 0; i < this->m_size; i++)
				this->m_elements[i] = rhs.m_elements[i];
		}
	}





	///
	template<class T>
	RingArray<T>::RingArray():
	m_size(0),
		m_elements(0),
		m_capacity(0),
		m_baseIndex(0),
		m_headIndex(0)
	{

	}


	///
	template<class T>
	RingArray<T>::RingArray(tSize capacity):
	m_size(0),
		m_baseIndex(0),
		m_headIndex(0)
	{
		this->Alloc(capacity);
	}


	///
	template<class T>
	RingArray<T>::RingArray(const Helper::RingArray<T> &rhs)
	{
		this->Copy(rhs);
	}





	///
	template<class T>
	void RingArray<T>::operator =(const Helper::RingArray<T> &rhs)
	{
		this->Copy(rhs);
	}


	///
	template<class T>
	T & RingArray<T>::operator [](tIndex index) const
	{
		tIndex temp = this->m_baseIndex + index;

		return this->m_elements[temp % this->m_capacity];
	}





	///
	template<class T>
	void RingArray<T>::SetCapacity(tSize capacity)
	{
		this->Delete();

		this->Alloc(capacity);
	}


	///
	template<class T>
	tSize RingArray<T>::Capacity() const
	{
		this->m_capacity;
	}


	///
	template<class T>
	bool RingArray<T>::IsEmpty() const 
	{
		return (this->m_size == 0);
	}


	///
	template<class T>
	void RingArray<T>::Reset()
	{
		this->m_size = 0;
		this->m_baseIndex = 0;
		this->m_headIndex = 0;
	}



	///
	template<class T>
	void RingArray<T>::Add(const T & element)
	{
		this->m_elements[this->m_headIndex++] = element;

		if(this->m_headIndex >= this->m_capacity)
			this->m_headIndex = 0;
		
		if(this->m_size == this->m_capacity)
		{
			++this->m_baseIndex;

			if(this->m_baseIndex >= this->m_capacity)
				this->m_baseIndex = 0;
		}
		else
			++this->m_size;
	}


	///
	template<class T>
	T & RingArray<T>::Front() const
	{
		return *(this->m_elements);
	}


	///
	template<class T>
	T & RingArray<T>::Back() const
	{
		return *(this->m_elements[this->m_size - 1]);
	}





	///
	template<class T>
	Array<T> RingArray<T>::GetAsArray() const 
	{
		Helper::Array<T> result(0, this->m_size);

		for(tIndex i = 0; i < this->m_size; i++)
			result.Add(*(this->m_elements[i]));

		return result;
	}


	///
	template<class T>
	const T * RingArray<T>::GetBuffer() const 
	{
		return this->m_elements;
	}


	///
	template<class T>
	T * RingArray<T>::GetBuffer()
	{
		return this->m_elements;
	}

}

#endif // ringarray_h__